package com.atcjx.pojo.Article;

import ch.qos.logback.core.joran.util.beans.BeanUtil;
import com.atcjx.pojo.IConverter;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.swing.plaf.basic.BasicInternalFrameTitlePane;
import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.List;

/**
 * @author admin
 * @version 1.0.0
 * @ClassName ArticleDTO.java
 * @Description 封装前端传来的Article类
 * @createTime 2021年09月20日 22:20:00
 */
@Data
@ApiModel(value = "前端文章类", description = "前端传入的文章信息")
public class ArticleDTO {
    @NotEmpty(message = "文章作者不能为空")
    @ApiModelProperty(notes = "文章作者", example = "竹林笔墨")
    private String author;
    @NotEmpty(message = "文章标题不能为空")
    @ApiModelProperty(notes = "文章标题", example = "快速入门SpringBoot")
    private String title;
    @NotEmpty(message = "文章内容不能为空")
    @ApiModelProperty(notes = "文章内容")
    private String content;
    @NotEmpty(message = "文章标签不能为空")
    @ApiModelProperty(notes = "文章标签，用英文逗号隔开", example = "java,集合")
    private String tags;
    @ApiModelProperty(notes = "文章类型，0表示转载，1表示原创", example = "1")
    private Integer type;
    @NotEmpty(message = "文章分类不能为空")
    @ApiModelProperty(notes = "文章分类", example = "设计模式")
    private String category;
    @NotEmpty(message = "文章简介不能为空")
    @ApiModelProperty(notes = "文章摘要")
    private String tabloid;
    @NotEmpty(message = "文章状态不能为空")
    @ApiModelProperty(notes = "文章状态")
    private String status;
    @ApiModelProperty(notes = "转载路径")
    private String reprintURL;

    @ApiModelProperty(notes = "封面图片的url")
    private String coverImg;

    private static class Converter implements IConverter<ArticleDTO, Article> {
        @Override
        public Article convertToVo(ArticleDTO articleDTO) {
            Article article = new Article();
            article.setGmtUpdate(System.currentTimeMillis());
            article.setReprintURL(articleDTO.reprintURL==null?"":articleDTO.reprintURL);
            BeanUtils.copyProperties(articleDTO, article);
            return article;
        }

        @Override
        public List<Article> convertToVO(List<ArticleDTO> t) {
            return null;
        }
    }
    /**
     * 描述：将DTO  转换为PO
     * @author 崔杰翔
     * @Date 2021/9/20 22:33
     * @param isUpdate  此对象是否为更新的对象
     * @return
     */
    public Article toArticlePo(boolean isUpdate){
        Article article=new Converter().convertToVo(this);
        List<String>tagsList=new ArrayList<>();
        // 参数转换
        for(String tags:this.tags.split(","))
            tagsList.add(tags);
        article.setArtTags(tagsList);
        article.setViews(isUpdate ? null : 0);
        article.setGmtCreate(isUpdate ? null : article.getGmtUpdate());
        return article;
    }
}
